Code
library(plotly)Bubbly, a delightful package from Plotly, brings life to your visualizations through interactive bubble charts. A bubble chart is a dynamic representation that goes beyond the conventional, allowing you to harness the third dimension effortlessly.
Bubble charts are a dynamic and visually compelling way to represent data, adding an extra layer of depth to your visual storytelling. Unlike traditional scatter plots, bubble charts introduce a third dimension by incorporating the size of bubbles, allowing you to convey complex information in a single glance.
Convey Complex Information: Bubble charts excel at conveying intricate data to your audience in a single, visually compelling snapshot.
Enhanced Interactivity: Bubbly charts engage your audience with interactive features, making data exploration an immersive experience.
Multidimensional Insights: Seamlessly represent multiple dimensions of data, offering a comprehensive view that traditional charts might miss.
library(plotly)# Create a simple bubble chart
data <- iris
bubble_chart <- plot_ly(
data = data,
x = ~Sepal.Length,
y = ~Sepal.Width,
marker = list(opacity = 0.7),
type = "scatter",
mode = "markers"
) %>%
layout(
xaxis = list(title = "Sepal Length"),
yaxis = list(title = "Sepal Width"),
title = "Bubble chart - plain"
)
# Display the chart
bubble_chart# Create a simple bubble chart with color and size
bubble_chart2 <- plot_ly(
data = data,
x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Species, # adding color
size = ~Petal.Length, # adding size
marker = list(opacity = 0.7, line = list(width = 0.5, color = 'white')),
type = "scatter",
mode = "markers"
) %>%
layout(
xaxis = list(title = "Sepal Length"),
yaxis = list(title = "Sepal Width"),
title = "Bubble chart - changed color and size of bubbles"
)
# Display the chart
bubble_chart2Now, when hovering over the point, all the details of that data point are displayed.
bubble_chart3 <- plot_ly(
data = data,
x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Species, # adding color
size = ~Petal.Length, # adding size
text = ~paste("Species: ", Species),
marker = list(opacity = 0.7),
type = "scatter",
mode = "markers"
) %>%
layout(
xaxis = list(title = "Sepal Length"),
yaxis = list(title = "Sepal Width"),
title = "Bubble chart - added hover text"
)
# Display the chart
bubble_chart3# load the suicide data
suicide <- read.csv("suicidedata.csv")
suicide <- na.omit(suicide)# create a interative plot
fig <- suicide %>%
plot_ly(
x = ~gdpPercap,
y = ~suicide_rate,
size = ~suicide_rate,
color = ~continent,
frame = ~Year,
text = ~Entity,
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
)
# change the layout
fig <- fig %>% layout(
xaxis = list(
title = "GDP per Capita (Log Scale)",
type = "log"
),
yaxis = list(
title = "Global suicide rate indicators"
),
title = "Suicide rates across the time",
annotations = list(
text = "Data Source: Our Data in World, 2022", # Add a caption
showarrow = FALSE,
xref = "paper",
yref = "paper",
x = 0,
y = 0
)
)
figBubble charts present versatile applications, and one notable scenario involves their usage in depicting word frequency, as illustrated in Figure 1.
In Figure 1, instead of employing a traditional word cloud, the use of a bubble chart proves to be a more effective means of conveying information. Bubbles serve as visual representations for each word, providing greater convenience for the audience in comprehending the data. The straightforward sizing of bubbles in this context is more intuitive than the varying font sizes typically used in a word cloud.
library(bubbles)
morandi_palette <- c(
"#D3C1C3", "#B4CCB9", "#A9B3C1", "#EFE1CE", "#C3D4C9",
"#BBAFAE", "#BFD3C1", "#D0C0A0", "#DBD3A3", "#D4C1A9",
"#A6937C", "#F2DCC4", "#C9CFC2", "#C9B5AC", "#E4C9B0",
"#B8C0A8", "#C4A77D", "#D1BCAF", "#CAB6A4", "#8D9E9A",
"#A4A4A9", "#B5A282", "#D3BBAF", "#C9D1D3","#DBD3A9"
)
# Create the bubble chart with customizations
bubbles(
value = df_bi$count2,
label = df_bi$Word,
col = morandi_palette
)Figure 1: Word Frequency in Depression Reddit Posts, Jenny Gong
It is also worth noting that in Figure 2, the package ggrepel is used to avoid overlapped text with automatic positioning.
Figure 2: European beer consumption
In this tutorial, we delved into the versatility of bubble charts in R and their efficacy in various scenarios. The interactive nature of these charts enhances communication by enabling users to dynamically explore data.
Emphasizing the importance of effective communication, we showcased that a well-crafted bubble chart transcends mere data representation; it narrates a compelling story. Whether visualizing trends or highlighting relationships, the bubble chart proves to be a valuable tool in data storytelling.
In conclusion, let’s bear in mind that the success of a visualization hinges on its ability to resonate with the audience. Craft your bubble charts to be clear, concise, and engaging for a meaningful impact. Happy charting!